package com.yxhl.zoume.core.tripsmgmt.presenter.detail;

import android.content.Context;
import android.text.TextUtils;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.ICancelOrderPresenter;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.ICommentPresenter;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.IRefundTicketPresenter;
import com.yxhl.zoume.core.tripsmgmt.view.unit.CancelOrderView;
import com.yxhl.zoume.core.tripsmgmt.view.unit.CommentOnDriverView;
import com.yxhl.zoume.core.tripsmgmt.view.unit.RefundTicketView;
import com.yxhl.zoume.data.http.model.trips.TripPassenger;
import com.yxhl.zoume.data.http.rest.param.specialcar.CancelOrderParam;
import com.yxhl.zoume.data.http.rest.param.tripsmgnt.CommentDriverParam;
import com.yxhl.zoume.data.http.rest.param.tripsmgnt.RefundTripParam;
import com.yxhl.zoume.data.http.rest.response.specialcar.CancelOrderResponse;
import com.yxhl.zoume.data.http.rest.response.tirps.CommentDriverResponse;
import com.yxhl.zoume.data.http.rest.response.tirps.RefundResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.tripsmgmt.CancelOrderUseCase;
import com.yxhl.zoume.domain.interactor.tripsmgmt.CommentDriverUseCase;
import com.yxhl.zoume.domain.interactor.tripsmgmt.TripRefundUseCase;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Subscription;

/**
 * Created by sugarya on 16/4/5.
 */
public class DetailTripPresenter extends BasePresenter implements ICommentPresenter, IRefundTicketPresenter, ICancelOrderPresenter {

    private static final String TAG = "HTDetailPresenter";

    @Inject
    CommentDriverUseCase mCommentDriverUseCase;
    @Inject
    TripRefundUseCase mTripRefundUseCase;
    @Inject
    CancelOrderUseCase mCancelOrderUseCase;

    private CommentOnDriverView mCommentOnDriverView;
    private RefundTicketView mRefundTicketView;
    private CancelOrderView mCancelOrderView;


    private Subscription mSubscription;

    @Inject
    public DetailTripPresenter(Context context) {
        super(context);
    }

    @Override
    public void commentOnDriver(String orderId, int starAmount, String commentMessage) {

        CommentDriverParam commentDriverParam = new CommentDriverParam(
                getMobile(),
                getToken(),
                orderId,
                starAmount,
                commentMessage
        );

        mCommentDriverUseCase.execute(commentDriverParam, new ZMSubscriber<CommentDriverResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
                mCommentOnDriverView.closeCommentOnCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mCommentOnDriverView.renderCommentOnError("");
            }

            @Override
            public void onNext(CommentDriverResponse commentDriverResponse) {
                super.onNext(commentDriverResponse);
                if (commentDriverResponse != null) {
                    boolean succ = commentDriverResponse.isSucc();
                    String resultMessage = commentDriverResponse.getResultMessage();
                    if (succ) {
                        mCommentOnDriverView.renderCommentOnSuccess();
                    } else {
                        mCommentOnDriverView.renderCommentOnError(resultMessage);
                    }
                }
            }
        });
    }

    @Override
    public void refundTicket(String orderId, final TripPassenger tripPassenger) {
        if (tripPassenger != null && !TextUtils.isEmpty(orderId)) {
            List<TripPassenger> tripPassengerList = new ArrayList<>();
            tripPassengerList.add(tripPassenger);
            RefundTripParam refundTripParam = new RefundTripParam(
                    getMobile(),
                    getToken(),
                    orderId,
                    tripPassengerList);

            mTripRefundUseCase.execute(refundTripParam, new ZMSubscriber<RefundResponse>() {
                @Override
                public void onCompleted() {
                    super.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }

                @Override
                public void onNext(RefundResponse refundResponse) {
                    super.onNext(refundResponse);
                    if (refundResponse != null) {
                        if (refundResponse.isSucc()) {
                            //关闭退票对话框,更新乘客信息列表的Adapter
                            mRefundTicketView.renderRefundOnSuccess();
                        } else {
                            String resultMessage = refundResponse.getResultMessage();
                            mRefundTicketView.renderRefundOnFailure(resultMessage);
                        }
                    }
                }
            });
        }
    }

    /**
     * 取消订单
     *
     * @param orderId
     */
    @Override
    public void cancelOrder(String orderId) {
        mCancelOrderView.waitingForOrderCancel(true);

        mCancelOrderUseCase.execute(new CancelOrderParam(getMobile(), getToken(), orderId), new ZMSubscriber<CancelOrderResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mCancelOrderView.waitingForOrderCancel(false);
            }

            @Override
            public void onNext(CancelOrderResponse cancelOrderResponse) {
                super.onNext(cancelOrderResponse);
                mCancelOrderView.waitingForOrderCancel(false);

                if (cancelOrderResponse.isSucc()) {
                    mCancelOrderView.renderCancelOrderResult(true);
                } else {
                    mCancelOrderView.renderCancelOrderResult(false);
                }
            }
        });
    }


    @Override
    public void onResume() {
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onDestroy() {
        mCancelOrderUseCase.unsubscribe();
        mCommentDriverUseCase.unsubscribe();
        mTripRefundUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        if (view != null) {
            if (view instanceof CommentOnDriverView) {
                mCommentOnDriverView = (CommentOnDriverView) view;
            }
            if (view instanceof RefundTicketView) {
                mRefundTicketView = (RefundTicketView) view;
            }
            if (view instanceof CancelOrderView) {
                mCancelOrderView = (CancelOrderView) view;
            }
        }
    }
}
